/**
 * 定义SafeDataModule系统数据管理模块
 */
(function(window, angular, undefined){
	var SafeDataModule = angular.module('SafeDataModule', ['ui.router','itfreer','waterCommon']);
	
	SafeDataModule.controller('SafeDataController',function($scope,$waterDataCommon,$httpData,$formObject,$rootScope,$wdp){
		$scope.roleList={};
		$scope.editUser = function(user){
			$rootScope.setEditRow('user', user);
			$rootScope.openWindow();
			$rootScope.setTab('user', 'tab1');
			$scope.getUserRole(user.S_USERID,user.S_ORGID);
		}
		
		$scope.getUserRole = function(userId,orgId){
			$scope.oid=orgId;
			var queryObj = {
				userId:userId,
				orgId:orgId
			};
			$waterDataCommon.getPageRows("/BaseInfo/queryUserRole.do","userRole",queryObj,1,1,function(data){
				$scope.roleList = data;
			});
		}
		//用于判断是否重新查询角色
		$scope.oid="";
		$scope.tabClick = function(userId,orgId){
			$rootScope.setTab('user', 'tab2');
			var arr=Object.keys($scope.roleList)
			//判断是否去重新查询角色
			if(arr.length>0&&$scope.oid==orgId){
				
			}
			else{
				$scope.getUserRole(userId,orgId);
			}
			
		}
		$scope.saveUser = function(){
			var user = $formObject.user;
			if(!user.editRow.C_USERNAME || !user.editRow.C_PWD || !user.editRow.C_USEREALNAME || !user.editRow.S_ORGID){
				$.messager.alert('系统提示',"带*号为必填项！");
				return ;
			}
			if(user.editRow.C_USERNAME=="" || user.editRow.C_PWD=="" || user.editRow.C_USEREALNAME=="" || user.editRow.S_ORGID==""){
				$.messager.alert('系统提示',"带*号为必填项！");
				return ;
			}
			if($formObject.user.editRow.childValues){
				$formObject.user.editRow.childValues.SAFE_USERROLE = [];
			}
			else{
				$formObject.user.editRow.childValues = {};
				$formObject.user.editRow.childValues.SAFE_USERROLE = [];
			}
			var userId = user.editRow.S_USERID;
			var roles = $("input[roleCheck='1']:checked");
			for(var i=0;i<roles.length;i++){
				var roleId = $(roles[i]).attr("s_roleid");
				$formObject.user.editRow.childValues.SAFE_USERROLE.push({
					FIELD_ISNEW:true,
					S_ROLEID:roleId,
					S_USERID:userId
				});
			}
			
			var queryObj = {
					S_USERID:userId,
					WECHATENTER_ID:$formObject.user.editRow.WECHATENTER_ID,
					WECHAT_ID:$formObject.user.editRow.WECHAT_ID,
					mobile:$formObject.user.editRow.C_USERTEL,
					email:$formObject.user.editRow.C_USEREMIL
			};
			$waterDataCommon.post("/SafeUser/saveUserWCID.do",queryObj,function(data){
				
				$rootScope.save('user', null, null, false, null, null, function(){
					$rootScope.closeWindow('edit');
					$.messager.alert("提示","保存成功","info");
				}, null);
			});
			
		}
		
		$scope.updateWCEnterID = function(){
			$formObject.user.editRow.WECHATENTER_ID = $formObject.user.editRow.WECHAT_ID + "_EnterID";
		};
	});
	
	SafeDataModule.controller('SafeRoleController',function($scope,$waterDataCommon,$httpData,$formObject,$rootScope,$wdp){
		
		$scope.stationItemSelect = function(item){
			if(item.ISCHECK == 0){
				item.ISCHECK = 1;
			}
			else{
				item.ISCHECK = 0;
			}
		}
		
		$scope.saveRoleInfo = function(){
			$rootScope.save('role');
			saveRoleStationInfo();
			$rootScope.closeWindow('edit');
		}
		
		$scope.setEdit = function(row){
			$scope.roleId = row.I_ROLEID;
			$rootScope.setEditRow('role', row);
			$rootScope.openWindow();
			$rootScope.setTab('role', 'tab1');
			getStation(row.I_ROLEID);
		}
		
		function saveRoleStationInfo(){
			var roleId = $scope.roleId;
			if(roleId == null){
				return;
			}
			var stationCodes = "";
			for(var i =0;i< $scope.stationToCheckList.length;i++){
				var station = $scope.stationToCheckList[i];
				if(station.ISCHECK == 1){
					stationCodes += station.CODE+",";
				}
			}
			var queryObj = {
					roleId:roleId,
					stationCodes:stationCodes
			};
			$waterDataCommon.post("/SafeRoleInfo/saveRoleStation.do",queryObj,function(data){
			});
		}
		
		function getStation(roleId){
			var queryObj = {
				roleId:roleId
			};
			$waterDataCommon.getPageRows("/SafeRoleInfo/getRoleStation.do","station",queryObj,1,1,function(data){
				$scope.stationToCheckList = data;
			});
		}
	});
}(window, angular));	